package com.ruoyi.web.controller.fs;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.fs.domain.FsCase;
import com.ruoyi.fs.domain.FsLawyer;
import com.ruoyi.fs.service.IFsCaseService;
import com.ruoyi.fs.service.IFsLawyerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fs.domain.FsTask;
import com.ruoyi.fs.service.IFsTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务Controller
 *
 * @author hero
 * @date 2024-09-03
 */
@RestController
@RequestMapping("/fs/task")
public class FsTaskController extends BaseController {
    @Autowired
    private IFsTaskService fsTaskService;
    @Autowired
    private IFsCaseService fsCaseService;
    @Autowired
    private IFsLawyerService fsLawyerService;

    /**
     * 查询任务列表
     */
    @PreAuthorize("@ss.hasPermi('fs:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsTask fsTask) {
        startPage();
        List<FsTask> list = fsTaskService.selectFsTaskList(fsTask);
        for (int i = 0; i < list.size(); i++) {
            FsTask task = list.get(i);
            FsCase aj = fsCaseService.selectFsCaseById(task.getCaseId());
            FsLawyer ls = fsLawyerService.selectFsLawyerById(task.getLsId());
            task.setAj(aj);
            task.setLs(ls);
            if (aj != null) {
                task.setAjMc(aj.getAjmc());
            }
            if (ls != null) {
                task.setLsMc(ls.getXm());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出任务列表
     */
    @PreAuthorize("@ss.hasPermi('fs:task:export')")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FsTask fsTask) {
        List<FsTask> list = fsTaskService.selectFsTaskList(fsTask);
        ExcelUtil<FsTask> util = new ExcelUtil<FsTask>(FsTask.class);
        util.exportExcel(response, list, "任务数据");
    }

    /**
     * 获取任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('fs:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fsTaskService.selectFsTaskById(id));
    }

    /**
     * 新增任务
     */
    @PreAuthorize("@ss.hasPermi('fs:task:add')")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsTask fsTask) {
        return toAjax(fsTaskService.insertFsTask(fsTask));
    }

    /**
     * 修改任务
     */
    @PreAuthorize("@ss.hasPermi('fs:task:edit')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsTask fsTask) {
        FsTask task = fsTaskService.selectFsTaskById(fsTask.getId());
        task.setRwzt(fsTask.getRwzt());
        task.setRemark(fsTask.getRemark());
        return toAjax(fsTaskService.updateFsTask(task));
    }

    /**
     * 删除任务
     */
    @PreAuthorize("@ss.hasPermi('fs:task:remove')")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fsTaskService.deleteFsTaskByIds(ids));
    }
}
